import apis from './api.js'
// let root = "https://test.caicaixiong.net"; // 测试
let root = "https://caicaixiong.net"; // 正式
// let root = "http://192.168.0.102:3000"
// let root = "https://bird.ioliu.cn/v2?url=https://caicaixiong.net"
let requests = {};
for (let api of apis) {
    requests[api.name] = (data,config={})=>{
        return new Promise((resolve,reject)=>{
            let userInfo = uni.getStorageSync("userInfo")
            if (config.loading) {
                uni.showLoading()
            }
            let header = {
                'REQUESTTYPES':1,
                'token':  "de84621733845c6c8676c47491c4cb8d" || userInfo.token || '',
            }
            if (api.header) {
                header = Object.assign(header,api.header)
            }
            if (data && data.uid) {
                data.uid = userInfo.id
            }
            uni.request({
                header,
                url: root+api.url,
                method: api.method || 'post',
                data,
                success: res=>{
                    let code = res.data.code;
                    if (code == 200) {
                        resolve(res.data)
                    } else if (code == 508) {
                        uni.showToast({
                            icon: "none",
                            title: "登录失效，请重新登录",
                        })
                        // setTimeout(()=>{
                        //     uni.navigateTo({
                        //         url: "/pages/login/login"
                        //     })
                        // },500)
                    } else {
                        uni.showToast({
                            icon: "none",
                            title: res.data.msg,
                        })
                    }
                    
                },
                fail: err=>{
                    reject(err.data)
                },
                complete: ()=>{
                    if (config.loading) {
                        uni.hideLoading()
                    }
                }
            })
        })
    }
}
export default requests;